package cate.game.util.api;

import cate.game.role.Role;
import cate.game.role.RoleRecordWriter;
import cp.solution.business.pay.PayRecordEntity;
import cp.solution.business.stat.pay.FirstPayLog;
import cp.solution.business.stat.role.RoleRecordEntity;
import cp.solution.rpc.path.RpcStatPath;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class StatApi {

    private static ExecutorService es = Executors.newSingleThreadExecutor();

    public static void updateRoleRecord(Role role) {
        if(role == null || role.getGame() == null) {
            return;
        }
        if(!role.getGame().isRunning()) {
            return;
        }
        try {
            RoleRecordEntity r = RoleRecordWriter.create(role);
            role.getGame().rpcSend().fireAndForgetStat(RpcStatPath.ROLE_RECORD_UPDATE, r);
        } catch (Exception e) {
            log.error("向StatServer updateRoleRecord 出错", e);
        }
    }

    public static void uploadFirstPayLog(Role role, PayRecordEntity record) {
        es.submit(() -> {
            try {
                FirstPayLog data = new FirstPayLog();
                data.setRoleId(record.roleId);
                data.setRoleName(record.roleName);
                data.setRoleLevel(record.roleLevel);
                data.setSrvId(record.srvId);
                data.setGameId(record.gameId);
                data.setGroupCode(record.srvGroupCode);
                data.setCreateTime(record.roleCreateTime);
                data.setPayTime(record.payTime);
                data.setItemTid(record.itemTid);
                data.setItemName(record.itemName);
                data.setItemNameCn(record.itemNameCn);
                data.setItemPrice(record.itemPrice);
                data.setItemNum(record.itemNum);
                data.setPayCents(record.payCents);
                data.complete();
                role.getGame().rpcSend().fireAndForgetStat(RpcStatPath.FIRST_PAY_LOG, data);
                log.info("首充汇报成功 {}", data);
            } catch (Exception e) {
                log.error("首充汇报失败", e);
            }
        });
    }
}
